import Vue from "vue";
import VueRouter from "vue-router";
import Main from "../views/Main.vue";
import fupin from "@/views/Fupin/fupin";
import yonghu from "@/views/Yonghu/yonghu";
import zhiyuanzhe from "@/views/Zhiyuanzhe/zhiyuanzhe";
import pinkunhu from "@/views/PinKunHu/index.vue";
import liuyanban from "@/views/LiuYanBan/index.vue";
import shoucang from "@/views/ShouCang/index.vue";
import Welcome from "@/compoents/server/Welcome.vue";
import Users from "@/compoents/server/Users/index";
import Home from "@/compoents/server/Home/index";
import Admin from "@/compoents/server/Admin/index";
import Login from "@/compoents/server/Login/index";
import Poverty from "@/compoents/server/Poverty/index";
import Policy from "@/compoents/server/Policy/index";
import Volunteer from "@/compoents/server/Volunteer/index";
import Message from "@/compoents/server/Message/index";
import LunBoTu from "@/views/LunBoTu/index.vue";
import ShouYe from "@/views/ShouYe/index.vue";
import Regist from "@/compoents/server/Regist/index";
import PovertySon from "@/compoents/server/Poverty/PovertySon/index";
import PolicySon from "@/compoents/server/Policy/PolicySon/index";

Vue.use(VueRouter);

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const routes = [
  {
    path: "/main",
    component: Main,
    children: [
      {
        path: "/shouye",
        component: ShouYe,
      },
      {
        path: "/shoucang",
        component: shoucang,
      },
      {
        path: "/fupin",
        component: fupin,
      },
      {
        path: "/yonghu",
        component: yonghu,
      },
      {
        path: "/zhiyuanzhe",
        component: zhiyuanzhe,
      },
      {
        path: "/pinkunhu",
        component: pinkunhu,
      },
      {
        path: "/liuyanban",
        component: liuyanban,
      },
      {
        path: "/lunbotu",
        component: LunBoTu,
      },
    ],
  },
  {
    path: "/",
    redirect: '/home',
    component: Welcome,
    children: [
      {
        path: "/user",
        name: "Users",
        component: Users,
      },
      {
        path: "/home",
        name: "Home",
        component: Home,
      },
      {
        path: "/admin",
        name: "Admin",
        component: Admin,
      },
      {
        path: "/poverty",
        component: Poverty,
      },
      {
        path: "/poverty/povertyson",
        component: PovertySon,
      },
      {
        path: "/policy",
        name: "Policy",
        component: Policy,
      },
      {
        path: "/policy/policyson",
        name: "PolicySon",
        component: PolicySon,
      },

      {
        path: "/volunteer",
        name: "Volunteer",
        component: Volunteer,
      },
      {
        path: "/message",
        name: "Message",
        component: Message,
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/regist",
    name: "Regist",
    component: Regist,
  },
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  // 如果用户访问登录页，直接放 
  if (to.path == "/login" || to.path == "/regist") return next();
  // 从sessionStorage 中获取保存的token 值
  const token = window.sessionStorage.getItem("token");
  // 没有token 强制跳转到登录页
  if (!token) return next("/login");
  next();
});

export default router;
